package com.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.common.annotation.Log;
import com.common.core.controller.BaseController;
import com.common.core.domain.AjaxResult;
import com.common.core.page.TableDataInfo;
import com.common.enums.BusinessType;
import com.common.utils.poi.ExcelUtil;
import com.domain.bo.EthicalReviewBo;
import com.domain.entity.EthicalReview;
import com.domain.vo.EthicalListVo;
import com.service.IEthicalReviewService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 伦理审查Controller
 *
 * @author ruoyi
 * @date 2025-05-27
 */
@Api(tags = "伦理审查")
@RestController
@RequestMapping("/ethical/review")
public class EthicalReviewController extends BaseController {

    @Autowired
    private IEthicalReviewService ethicalReviewService;

    /**
     * 列表左上方统计
     */
    @ApiOperation("列表左上方统计")
    @GetMapping("/groups")
    public AjaxResult groups(EthicalReviewBo ethicalReviewBo){
        return success(ethicalReviewService.selectGroups(ethicalReviewBo));
    }

    /**
     * 查询伦理审查列表
     */
    @ApiOperation("查询伦理审查列表")
    @GetMapping("/list")
    public TableDataInfo list(EthicalReviewBo ethicalReviewBo)
    {
        IPage<EthicalListVo> proList = ethicalReviewService.selectList(ethicalReviewBo,"list");
        return TableDataInfo.build(proList.getRecords(),proList.getTotal());
    }

    /**
     * 导出伦理审查列表
     */
    @ApiOperation("导出伦理审查列表")
    @Log(title = "伦理审查", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, @RequestBody EthicalReviewBo ethicalReviewBo)
    {
        IPage<EthicalListVo> list = ethicalReviewService.selectList(ethicalReviewBo,"export");
        ExcelUtil<EthicalListVo> util = new ExcelUtil<>(EthicalListVo.class);
        util.showColumn(ethicalReviewBo.getExportStr().split(","));
        util.exportExcelByOrder(response, list.getRecords(), "伦理审查数据",ethicalReviewBo.getExportStr().split(","));
    }

    /**
     * 获取伦理审查详细信息
     */
    @ApiOperation("获取伦理审查详细信息")
    @GetMapping(value = "/{ID}")
    public AjaxResult getInfo(@PathVariable("ID") String ID)
    {
        return success(ethicalReviewService.selectEthicalReviewByID(ID));
    }

    /**
     * 保存伦理审查
     */
    @ApiOperation("保存伦理审查")
    @PostMapping("/saveEthical")
    public AjaxResult saveEthical(@RequestBody EthicalReview ethicalReview)
    {
        return ethicalReviewService.saveEthicalReview(ethicalReview);
    }

    /**
     * 删除伦理审查
     */
    @ApiOperation("删除伦理审查")
    @Log(title = "伦理审查", businessType = BusinessType.DELETE)
    @DeleteMapping("/{IDs}")
    public AjaxResult remove(@PathVariable String[] IDs)
    {
        String res = ethicalReviewService.deleteEthicalReviewByIDs(IDs);
        if(res.contains("成功")){
            return success(res);
        }else{
            return error(res);
        }
    }
}
